package KiemTra;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BaiTap001 extends JFrame{
	JTextField[] txt = new JTextField[20];	
	public void Fibonacci(){
		long[] a = new long[20];
		a[0] = 0; a[1] = 1;
		for(int i = 2; i < 20; i++){
			a[i] = a[i-1] + a[i-2];
		}
		for(int j = 0; j < 20; j++){
			txt[j].setText(String.valueOf(a[j]));
		}
	}	
	public static boolean IsNguyenTo(long n){
		boolean resuft = true;
		if(n >= 2){
			for(int i = 2; i < n; i++){
				if(n % i == 0) resuft = false;
			}
		}
		else resuft = false;
		return resuft;
	}	
	public void SoNguyenTo(){
		long[] a = new long[20];
		int i = 0;
		long n = 0;
		do {
			if(IsNguyenTo(n)){
				a[i] = n;
				i++;
			}
			n++;
		}
		while(i < 20);		
		for(int j = 0; j < 20; j++){
			txt[j].setText(String.valueOf(a[j]));
		}
	}	
	public BaiTap001(){
		setTitle("Bai 1");
		setSize(500, 150);
		setVisible(true);
		JLabel td = new JLabel("SỐ NGUYÊN TỐ - FIBONACCI");
		JPanel ptd = new JPanel();
		ptd.add(td);
		
		add(ptd, BorderLayout.NORTH);		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,20));
		for(int i = 0; i< 20; i++){
			txt[i] = new JTextField();
			p.add(txt[i]);
		}
		add(p, BorderLayout.CENTER);	
		
		JButton nutSNT = new JButton("In 20 số nguyên tố"), 
				nutFib= new JButton("In 20 số Fibonacci");
		
		JPanel pNut = new JPanel();
		pNut.add(nutSNT);
		pNut.add(nutFib);
		add(pNut, BorderLayout.SOUTH);
		
		nutSNT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SoNguyenTo();
			}
		});
		 nutFib.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Fibonacci();
			}
		});
	}
	public static void main(String[] args) {
		new BaiTap001();
	}
}
